package com.htfyun.fbreader;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.htfyun.utils.DebugLog;

import org.geometerplus.android.fbreader.FBReader;
import org.geometerplus.android.fbreader.NavigationWindow;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.zlibrary.core.application.ZLApplication;
import org.geometerplus.zlibrary.text.view.ZLTextWordCursor;
import org.geometerplus.zlibrary.ui.android.R;

/**
 * Created by sst on 2017/11/2.
 */

public class HTFYunMenuPopup_new extends ZLApplication.PopupPanel{
    public final static String ID = "HTFYunMenuPopup";

    private volatile NavigationWindow myWindow;
    private volatile FBReader myActivity;
    private volatile RelativeLayout myRoot;
    private ZLTextWordCursor myStartPosition;
    private final FBReaderApp myFBReader;
    private volatile boolean myIsInProgress;

    public HTFYunMenuPopup_new(FBReaderApp fbReader) {
        super(fbReader);
        myFBReader = fbReader;
    }

    public void setPanelInfo(FBReader activity, RelativeLayout root) {
        myActivity = activity;
        myRoot = root;
    }

    public void runMenuPopup() {
        if (myWindow == null || myWindow.getVisibility() == View.GONE) {
            myIsInProgress = false;
            if (myStartPosition == null) {
                myStartPosition = new ZLTextWordCursor(myFBReader.getTextView().getStartCursor());
            }
            Application.showPopup(ID);
        }
    }

    @Override
    protected void show_() {
        if (myActivity != null) {
            createPanel(myActivity, myRoot);
        }
        if (myWindow != null) {
            myWindow.show();
//            setupNavigation();
        }
    }

    public void show() {
        show_();
    }

    @Override
    protected void hide_() {
        if (myWindow != null) {
            myWindow.hide();
        }
    }

    @Override
    public String getId() {
        return ID;
    }

    @Override
    protected void update() {
        if (!myIsInProgress && myWindow != null) {
//            setupNavigation();
        }
    }

    private void createPanel(final FBReader activity, RelativeLayout root) {
        if (myWindow != null && activity == myWindow.getContext()) {
            return;
        }

        activity.getLayoutInflater().inflate(R.layout.htfyun_menu_panel, root);
        myWindow = (NavigationWindow)root.findViewById(R.id.htfyun_menu_panel);

        root.findViewById(R.id.imgFontSizeSmall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DebugLog.e("123");
            }
        });
        
        
    }



    public final void removeWindow(Activity activity) {
        if (myWindow != null && activity == myWindow.getContext()) {
            final ViewGroup root = (ViewGroup)myWindow.getParent();
            myWindow.hide();
            root.removeView(myWindow);
            myWindow = null;
        }
    }
}
